package com.lsfb.duishangbaos.Operate;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.lsfb.duishangbaos.R;
import com.lsfb.duishangbaos.Utils.LittleUtils;
import com.lsfb.duishangbaos.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.SN;

import java.util.HashMap;
import java.util.Hashtable;


/**
 * 经营页面
 * Created by Administrator on 2017/5/10 0010.
 */
@ContentView(R.layout.aty_operate)
public class OperateActivity extends LsfbActivity {

    @ViewInject(R.id.aty_img_erweima)
    private ImageView img_erweima;

    @ViewInject(R.id.aty_text_money)
    private TextView text_money;

    private String money = "";

    private Bitmap qrBitmap;

    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case 1000:
                    text_money.setText(money);
                    break;
                case 2000:
                    img_erweima.setImageBitmap(qrBitmap);
                    break;
            }
        }
    };

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "经营");
    }

    @Override
    public void initEvent() {

    }

    public Bitmap createQRImage(String url, final int width, final int height) {
        try {
            // 判断URL合法性
            if (url == null || "".equals(url) || url.length() < 1) {
                return null;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            // 图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(url,
                    BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            // 下面这里按照二维码的算法，逐个生成二维码的图片，
            // 两个for循环是图片横列扫描的结果
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * width + x] = 0xff000000;
                    } else {
                        pixels[y * width + x] = 0xffffffff;
                    }
                }
            }
            // 生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_jinyin_set_money})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.aty_jinyin_set_money:
                startActivity(new Intent(this, SetMoneyActivity.class));
                break;
        }
    }

    @Override
    public void initData() {
        getData();
    }


    public void getData() {

        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("cash", "0");
        new BaseInternet().getData(URLString.SETCASH, hashMap, SetCashBean.class, new SetCashEvent2<SetCashBean>(), true);
    }

    @EventAnnotation
    public void SetCashEvent(SetCashEvent2<SetCashBean> event) {
        switch (event.getCode()) {
            case 200:
                qrBitmap = createQRImage(event.getData().getKey(), 500, 500);
                img_erweima.setImageBitmap(qrBitmap);
                break;
            default:
                SN.SnackShow(img_erweima, event.getMsg());
                break;
        }
    }

    /**
     * 获取密文
     *
     * @param event
     */
    @EventAnnotation
    public void SetMoneyClickEvent(SetMoneyClickEvent event) {
        qrBitmap = createQRImage(event.getData(), 500, 500);

        handler.sendEmptyMessage(2000);
    }

    /**
     * 获取金额
     *
     * @param event
     */
    @EventAnnotation
    public void SetMoneyClickEvent2(SetMoneyClickEvent2 event) {
        money = event.getData();
        handler.sendEmptyMessage(1000);
    }

}
